<?php
namespace Admin\Controller;
use Think\Controller;
class StatusController extends CommonController{

	public function status(){
		if (IS_POST) {
			$list = I('post.');
            $arr= array('status_name');
            for ($i=0; $i < count($arr); $i++) {
                $$arr[$i] =$list[$arr[$i]]  ? $list[$arr[$i]] :$this->error($arr[$i].'不能为空');
            }
            $data = M('device_status')->add($list);
            
            if ($data) {
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
		}
		$data = M('device_cate')->field('id,cate_name')->select();
		$this->assign("cate", $data);
		$this->display();
	}

	public function statusTwo(){
		if (IS_POST) {
			$list = I('post.');
            $arr= array('status_name','status_id');
            for ($i=0; $i < count($arr); $i++) {
                $$arr[$i] =$list[$arr[$i]]  ? $list[$arr[$i]] :$this->error($arr[$i].'不能为空');
            }
            $status = array(
            	'st_id'=>$status_id,
            	'fu_id'=>0,
            	'name' =>$status_name,
            	);
            $data = M('device_status_next')->add($status);
            
            if ($data) {
            	M('device_status')->where('id='.$status_id)->save(array('is_next'=>1));
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
		}

		$data = M('device_cate')->field('id,cate_name')->select();
		$this->assign("cate", $data);
		$this->display();
	}

	public function statusSt(){
		if (IS_POST) {
			$list = I('post.');
            $arr= array('status_name','status_id','status_ids');
            for ($i=0; $i < count($arr); $i++) {
                $$arr[$i] =$list[$arr[$i]]  ? $list[$arr[$i]] :$this->error($arr[$i].'不能为空');
            }
            $status = array(
            	'st_id'=>$status_id,
            	'fu_id'=>$status_ids,
            	'name' =>$status_name,
            	);
            $data = M('device_status_next')->add($status);
            
            if ($data) {
            	M('device_status_next')->where('id='.$status_ids)->save(array('is_next'=>1));
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
		}

		$data = M('device_cate')->field('id,cate_name')->select();
		$this->assign("cate", $data);
		$this->display();
	}


	// 根据 cate_id 来找 设备情况
	public function statusInfo(){
		$cate_id = I('post.id');

		$data = M('device_status')->where('cate_id='.$cate_id)->field('id,status_name')->select();

		$opt = '<option>--请选择--</option>';
		foreach($data as $key=>$val){
		    $opt .= "<option value='{$val['id']}'>{$val['status_name']}</option>";
	 	}

	 	$this->ajaxReturn($opt);
	}

	// 根据设备情况id 找设备情况的二级目录
	public function statusNext(){
		$id = I('post.id');
		$data = M('device_status_next')->where('st_id='.$id.' AND fu_id=0')->field('id,name')->select();
		if (!$data) 
		{
			$opt = '<option>该项类没有二级分类</option>';
		}else
		{
			$opt = '<option>--请选择--</option>';
		}
		
		foreach($data as $key=>$val){
		    $opt .= "<option value='{$val['id']}'>{$val['name']}</option>";
	 	}

	 	$this->ajaxReturn($opt);
	}

	// 根据设备情况二级目录id 找设备情况的三级目录
	public function statusNexts(){
		$id = I('post.id');
		$data = M('device_status_next')->where('fu_id='.$id)->field('id,name')->select();
		if (!$data) {
			$opt = '<option>该项类没有三级分类</option>';
		}else
		{
			$opt = '<option>--请选择--</option>';
		}
		foreach($data as $key=>$val){
		    $opt .= "<option value='{$val['id']}'>{$val['name']}</option>";
	 	}

	 	$this->ajaxReturn($opt);
	}
}